package cc.rengu.igas.smps.core.service.trans;

import cc.rengu.igas.smps.common.constant.SmpsParamConstant;
import cc.rengu.igas.smps.common.constant.SmpsServiceConstant;
import cc.rengu.igas.smps.facade.request.GeneralSmsNoticeRequest;
import cc.rengu.igas.smps.facade.response.GeneralSmsNoticeResponse;
import cc.rengu.jradp.schedule.ScheduleImpl;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.utility.base.StringUtil;

import java.util.List;

/**
 * 短信通知
 * Created by wangcheng on 2021/3/31 14:08
 */
public class SmsNoticeService extends RadpService {
    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {
        GeneralSmsNoticeRequest generalSmsNoticeRequest = new GeneralSmsNoticeRequest();
        GeneralSmsNoticeResponse generalSmsNoticeResponse = new GeneralSmsNoticeResponse();
        try {
            ConvertUtil.convertOutput(generalSmsNoticeRequest);
            generalSmsNoticeResponse.setHeader(generalSmsNoticeRequest.getHeader());
            String templateId = generalSmsNoticeRequest.getTemplateId();
            String realtimeFlag = generalSmsNoticeRequest.getRealtimeFlag();
            String appointTime = generalSmsNoticeRequest.getAppointTime();
            List<String> phoneList = generalSmsNoticeRequest.getPhoneList();
            String smsContent = generalSmsNoticeRequest.getSmsContent();
            if (StringUtil.isNullorEmpty(templateId) || StringUtil.isNullorEmpty(realtimeFlag)
                    || StringUtil.isNullorEmpty(smsContent) || null == phoneList) {
                rglog.error("缺少一个或多个业务参数！");
                generalSmsNoticeResponse.setRespCode(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespCode());
                generalSmsNoticeResponse.setRespDesc(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespDesc());
            } else {
                /* 演示环境发送短信默认返回成功 */
                if (AppParamConstant.DEFAULT_ENV.equals(AppParamConstant.APPLICATION_ENV)) {
                    generalSmsNoticeResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
                    generalSmsNoticeResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
                } else {
                    int iReturnCode = schedule.callService(SmpsServiceConstant.SMS_COMMUNICATION_CLIENT, SmpsParamConstant.DEFAULT_SERVICE_LABEL);
                    if (ScheduleImpl.SERVICE_SUCCESS == iReturnCode) {
                        generalSmsNoticeResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
                        generalSmsNoticeResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
                    } else if (ScheduleImpl.SERVICE_TIMEOUT == iReturnCode) {
                        rglog.error("callService {} 失败！返回值={}", SmpsServiceConstant.SMS_COMMUNICATION_CLIENT, iReturnCode);
                        generalSmsNoticeResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                        generalSmsNoticeResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
                    } else {
                        rglog.error("callService {} 失败！返回值={}", SmpsServiceConstant.SMS_COMMUNICATION_CLIENT, iReturnCode);
                        generalSmsNoticeResponse.setRespCode(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode());
                        generalSmsNoticeResponse.setRespDesc(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
                    }
                }
            }
            ConvertUtil.convertInput(generalSmsNoticeResponse);
        } catch (Exception e) {
            //失败赋值应答码为明确失败及应答消息直接退出
            rglog.error("{}", StringUtil.ExceptionToString(e));
            generalSmsNoticeResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            generalSmsNoticeResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            return -1;
        }

        return 0;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }
}
